/*************************************************************************
	> File Name: 1.solve-2.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2024-12-27 14:39:53
 ************************************************************************/

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX_V 100000
int dp[MAX_V + 5];

int main() {
    int V, n;
    cin >> V >> n;
    for (int i = 0, v, w, s; i < n; i++) {
        cin >> v >> w >> s;
        for (int k = 1; s; s -= k, k *= 2) {
            k = min(s, k);
            for (int j = V; j >= k * v; j--) {
                dp[j] = max(dp[j], dp[j - k * v] + k * w);
            }
        }
    }
    cout << dp[V] << endl;
    return 0;
}
